package com.example.hanfu_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hanfu_backend.entity.HanfuType;
import com.example.hanfu_backend.mapper.HanfuTypeMapper;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hanfuType")
public class HanfuTypeController {
    @Autowired
    HanfuTypeMapper hanfuTypeMapper;

    @GetMapping("/getAllTypes")
    @Operation(summary = "不分页查询所有种类")
    public List<HanfuType> getAllTypes() {
        return hanfuTypeMapper.selectList(null);
    }

    @GetMapping("/getTypes")
    @Operation(summary = "分页查询所有种类")
    public Map<String,Object> getTypes(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize) {
        Page<HanfuType> page = new Page<>(pageNum,pageSize); // 创建分页对象
        // 设置分页查询条件
        QueryWrapper<HanfuType> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("hanfutype_id");
        IPage<HanfuType> iPage = hanfuTypeMapper.selectPage(page,queryWrapper); // 执行分页查询操作
        // 封装分页查询结果
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("records",iPage.getRecords());
        return map;
    }

    @DeleteMapping("/deleteTypes")
    @Operation(summary = "根据ID删除某一类型")
    public int deleteType(@RequestParam("hanfuTypeId")Integer hanfuTypeId) {
        return hanfuTypeMapper.deleteById(hanfuTypeId);
    }

    @PutMapping("/updateTypes")
    @Operation(summary = "根据Id修改某一类型")
    public int updateType(@RequestBody HanfuType hanfuType) {
        return hanfuTypeMapper.updateById(hanfuType);
    }

    @PostMapping("/insertType")
    @Operation(summary = "添加类型")
    public int insertType(@RequestBody HanfuType hanfuType) {
        return hanfuTypeMapper.insert(hanfuType);
    }

    @GetMapping("/getTypeById")
    @Operation(summary = "通过种类ID分页查询种类")
    public Map<String,Object> queryTypeById(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize,@RequestParam("hanfuTypeId")Integer hanfuTypeId) {
        Page<HanfuType> page = new Page<>(pageNum,pageSize);
        QueryWrapper<HanfuType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hanfutype_id",hanfuTypeId);
        IPage<HanfuType> iPage = hanfuTypeMapper.selectPage(page,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("records",iPage.getRecords());
        return map;
    }

    @GetMapping("/getTypeByName")
    @Operation(summary = "通过种类名称分页模糊查询种类")
    public Map<String,Object> queryTypeByName(@RequestParam("pageNum")int pageNum,@RequestParam("pageSize")Integer pageSize,@RequestParam("hanfuTypeName")String hanfuTypeName) {
        Page<HanfuType> page = new Page<>(pageNum,pageSize);
        QueryWrapper<HanfuType> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("hanfutype_name",hanfuTypeName);
        IPage<HanfuType> iPage = hanfuTypeMapper.selectPage(page,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("records",iPage.getRecords());
        return map;
    }
}
